/*
 *  Copyright 2011 Iurii Dunko, Iana Potochniak.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.yyminijava.syntaxtree;

import org.opu.yyminijava.visitor.Visitor;

/**
 * 
 * @author Iurii Dunko
 * @author Iana Potochniak
 */
public class ArrayCreationExpression implements CreationExpression {

	private static final long serialVersionUID = 1323110697215L;

	private Type type;
	private Expression expression;
	/**
	 * @Deprecated we don't support declaration of not dimensional array, so we
	 *             don't need not dimensional initialization
	 */
	private int dimension = 0;

	public ArrayCreationExpression(Type type, Expression expression,
			int dimension) {
		this.type = type;
		this.expression = expression;
		this.dimension = dimension;
	}

	public ArrayCreationExpression(Type type, Expression expression) {
		this.type = type;
		this.expression = expression;
	}

    public ArrayCreationExpression() {
    }

    @Override
    public Type getType() {
        ArrayType arrayType = new ArrayType(type, 1);
        arrayType.initialisationComplete();
		return arrayType;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Expression getExpression() {
		return expression;
	}

	public void setExpression(Expression expression) {
		this.expression = expression;
	}

	public int getDimension() {
		return dimension;
	}

	public void setDimension(int dimension) {
		this.dimension = dimension;
	}

    public void increaseDimension(){
        dimension ++;
    }

    public void validate(){
        if (dimension > 0){
            // TODO: create own exception struction!
            // Using error is temporary solution, we use exception when the
            // incoming file contains errors, when the problem in our compiler
            // we use errors (but it for now)
            throw new Error("Current version support only dimensional array");
        }
    }

	@Override
	public String toString() {
		return String.valueOf(type) + "[" + expression + "]";
	}

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(type);
        visitor.visit(expression);
    }

}
